package com.panshi.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.panshi.modules.system.bean.BeiSysUser;
import com.panshi.modules.system.mapper.BeiSysUserMapper;
import com.panshi.modules.system.service.BeiSysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;



import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后端管理用户  服务实现类
 * </p>
 *
 * @author 小伍
 * @since 2020-08-05
 */
@Service
public class BeiSysUserServiceImpl implements BeiSysUserService {

    @Autowired
    private  BeiSysUserMapper beiSysUserMapper;

    @Override
    @Cacheable(value = "user", key = "#root.methodName+#username" )
    public BeiSysUser querySysUser(String username) {
        QueryWrapper<BeiSysUser> user=new QueryWrapper<>();
        user.select("uaid","nickname","password","username","gender","phone","email","brief","head","last_login_time","realname");
        user.eq("username",username);
        return beiSysUserMapper.selectOne(user);
    }

    @Override
    @Cacheable(value = "user", key = "#root.methodName+#map" )
    public List<BeiSysUser> queryAllSys(Map<String,Object> map) {
        return beiSysUserMapper.queryAllSys(map);
    }

    @Override
    @Cacheable(value = "user", key = "#root.methodName" )
    public Integer querySysUserCount() {
        QueryWrapper<BeiSysUser> user=new QueryWrapper<>();
        user.isNotNull("uaid");
        return beiSysUserMapper.selectCount(user);
    }

    @Override
    @CacheEvict(value="user",allEntries=true)
    public int addSysUser(BeiSysUser beiSysUser) {
        beiSysUserMapper.insert(beiSysUser);
        return beiSysUser.getUaid();
    }

    @Override
    @Cacheable(value = "user",  key = "targetClass + methodName +#p0")
    public List<BeiSysUser> querySys(Map<String, Integer> map) {
        Page<BeiSysUser> page = new Page(map.get("currentPage"),map.get("pageSize"));
        QueryWrapper<BeiSysUser> user=new QueryWrapper<>();
        user.select("uaid","nickname","password","username","gender","phone","email","brief","head","last_login_time","realname");
        beiSysUserMapper.selectPage(page, user);
        return  page.getRecords();
    }

    @Override
    @CacheEvict(value="user",allEntries=true)
    public void updateSysUser(BeiSysUser beiSysUser) {
        beiSysUserMapper.updateById(beiSysUser);
    }

    @Override
    @CacheEvict(value="user",allEntries=true)
    public void deleteSysUser(Integer uid) {
        beiSysUserMapper.deleteById(uid);
    }


}
